package lk;

import java.util.ArrayList;
import java.util.List;

public class D230928 {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    // 二叉树的最大深度
    // 时间：O(n)
    // 空间：O(height)
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        int height = leftHeight > rightHeight ? leftHeight : rightHeight;

        return height + 1;
    }

    // 叶子相似的树
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> list1 = new ArrayList<>();
        if (root1 != null) {
            dfs(root1, list1);
        }

        List<Integer> list2 = new ArrayList<>();
        if (root1 != null) {
            dfs(root2, list2);
        }

        return list1.equals(list2);
    }

    public void dfs(TreeNode node, List<Integer> list) {
        if (node.left == null && node.right == null) {
            list.add(node.val);
        } else {
            if (node.left != null) {
                dfs(node.left, list);
            }
            if (node.right != null) {
                dfs(node.right, list);
            }
        }
    }
}
